package org.itboys.mobile.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.dto.product.SundayHouseTypeDto;
import org.itboys.mobile.entity.mongo.SundayHouseType;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/7/17
 * 描述：
 */
@Service
public class SundayHouseTypeService extends BaseMongoService<SundayHouseType>{
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private SundayUploadService uploadFileService;
    @Autowired
    private ResourceHolder resourceHolder;
    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }
    @Override
    protected Class<SundayHouseType> getEntityClass() {
        return SundayHouseType.class;
    }

    public void save(SundayHouseType houseType, MultipartFile imageFile) throws IOException {
        Map<String, String> backMap = uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(backMap.get(uploadService.UPLOAD_HALF_PATH))){
            houseType.setImage(backMap.get(uploadService.UPLOAD_HALF_PATH));
        }
        //编辑
        if(LongUtil.isNotZreo(houseType.getId())){
            super.updateExceptEmpty(houseType.getId(),houseType);
        }else{
            super.save(houseType);
        }
    }

    public PageResult<SundayHouseType> select(HttpServletRequest request,Long productId){
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request, "name");
        Map<String,Object> param = new HashMap<>();
        param.put("isDeleted", 0);
        if(LongUtil.isNotNull(productId)){
            param.put("productId",productId);
        }
        PageResult<SundayHouseType> result=super.containpageQuery(request,containsparam,param);
        return result;
    }
    public SundayHouseType findOne(Long id){
        SundayHouseType houseType=null;
        if(LongUtil.isNotZreo(id)){
            houseType=super.getById(id);
        }else {
            houseType=new SundayHouseType();
        }
        return houseType;
    }

    public List<SundayHouseTypeDto> packageDtoMuilt(List<SundayHouseType> houseTypes){
        List<SundayHouseTypeDto> dtos=new ArrayList<SundayHouseTypeDto>();
        String imageRoot = resourceHolder.getStringValue("imgRoot");
        for (SundayHouseType houseType :houseTypes) {
            SundayHouseTypeDto dto=new SundayHouseTypeDto(houseType.getName(),imageRoot+houseType.getImage(),
                    houseType.getStandard(), houseType.getDirection(), houseType.getAcreage());
            dtos.add(dto);
        }
        return dtos;
    }
}
